package com.qdxwx.data.service.order;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.mapper.order.OrderDetailMapper;
import com.qdxwx.models.order.OrderDetail;
import com.qdxwx.models.order.OrderDetailQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * OrderDetail接口
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class OrderDetailServiceImpl implements OrderDetailService {

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * 添加OrderDetail
     *
     * @param orderDetail 要添加的OrderDetail
     */
    @Override
    public int insert(OrderDetail orderDetail) {
        if (orderDetail.getCreateTime() == null) {
            orderDetail.setCreateTime(LocalDateTime.now());
        }
        orderDetail.setUpdateTime(LocalDateTime.now());
        return orderDetailMapper.createOrderDetail(orderDetail);
    }

    /**
     * 批量添加OrderDetail
     */
    @Override
    public void addBatchOrderDetail(List<OrderDetail> orderDetail) {
        orderDetailMapper.addBatchOrderDetail(orderDetail);
    }

    /**
     * 根据id删除一个OrderDetail
     */
    @Override
    public void delete(Long id) {
        orderDetailMapper.deleteOrderDetailById(id);
    }

    /**
     * 修改OrderDetail
     *
     * @param orderDetail 要修改的OrderDetail
     */
    @Override
    public int update(OrderDetail orderDetail) {
        orderDetail.setUpdateTime(LocalDateTime.now());
        return orderDetailMapper.updateOrderDetail(orderDetail);
    }

    /**
     * 根据id获取单个OrderDetail对象
     */
    @Override
    public OrderDetail get(Long id) {
        return orderDetailMapper.selectOrderDetailById(id);
    }

    /**
     * 根据条件获取OrderDetail列表
     */
    @Override
    public List<OrderDetail> getList(OrderDetailQuery query) {
        return getList(query, null);
    }

    @Override
    public List<OrderDetail> getListByOrderId(Integer orderId) {
        OrderDetailQuery query = new OrderDetailQuery();
        query.setOrderId(orderId);
        return orderDetailMapper.selectList(query, null);
    }

    @Override
    public List<OrderDetail> getList(OrderDetailQuery orderDetail, PageRequest page) {
        return orderDetailMapper.selectList(orderDetail, page);
    }

    @Override
    public long count(OrderDetailQuery orderDetail) {
        return orderDetailMapper.count(orderDetail);
    }

    @Override
    public int updateOrderDetailStatusSuccess(OrderDetail orderDetail) {
        orderDetail.setUpdateTime(LocalDateTime.now());
        return orderDetailMapper.updateOrderDetailStatusSuccess(orderDetail);
    }

    @Override
    public List<OrderDetail> getTimeOverOrder() {
        return orderDetailMapper.selectTimeOverOrder();
    }

    @Override
    public OrderDetail getByOrderId(int orderId) {
        return orderDetailMapper.selectByOrderId(orderId);
    }

    @Override
    public long sumPrice(OrderDetailQuery orderDetail) {
        Long sum = orderDetailMapper.sumPrice(orderDetail);
        if (sum == null) {
            sum = 0L;
        }
        return sum;
    }

    @Override
    public long countByCourse(OrderDetailQuery orderDetail) {
        return orderDetailMapper.countByCourse(orderDetail);
    }

    @Override
    public List<OrderDetail> getListByCourse(OrderDetailQuery query, PageRequest page) {
        return orderDetailMapper.selectListByCourse(query, page);
    }

    @Override
    public int updateCombine(long userId1, long userId2) {
        if (userId1 < 1 || userId2 < 1) {
            throw new RuntimeException("更新订单详情：参数错误");
        }
        return orderDetailMapper.updateCombine(userId1, userId2);
    }
}